package com.gufe.result;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.time.LocalDateTime;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class WxMessageResult<T> implements Serializable {
    private String senderId;//发送方id
    private String content;//消息内容
    private LocalDateTime time;//发送时间
    private Integer delieveStatus;  // 状态字段 1:表示发送，2：表示反馈成功，3：表示反馈失败
    private String msgId;//消息id


    public static TextWebSocketFrame delieveMessage(String fromUserId,String msgContent) throws JsonProcessingException {
        WxMessageResult  result = new WxMessageResult();
        result.senderId = fromUserId;
        result.content = msgContent;
        result.time = LocalDateTime.now();
        result.delieveStatus = 1;
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new JavaTimeModule()); // 注册 JavaTimeModule
        mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); // 禁用时间戳格式
        String jsonMessage = mapper.writeValueAsString(result);
        return new TextWebSocketFrame(jsonMessage);
    }

    public static TextWebSocketFrame responseSuccess(String msgId) throws JsonProcessingException {
        WxMessageResult result = new WxMessageResult();
        result.senderId = "system";
        result.delieveStatus = 2;
        result.msgId = msgId;
        ObjectMapper mapper = new ObjectMapper();
        String jsonMessage = mapper.writeValueAsString(result);
        return new TextWebSocketFrame(jsonMessage);
    }

    public static TextWebSocketFrame responseErro(String msgId) throws JsonProcessingException {
        WxMessageResult result = new WxMessageResult();
        result.senderId = "system";
        result.delieveStatus = 3;
        result.msgId = msgId;
        ObjectMapper mapper = new ObjectMapper();
        String jsonMessage = mapper.writeValueAsString(result);
        return new TextWebSocketFrame(jsonMessage);
    }

    public static TextWebSocketFrame responseErro() throws JsonProcessingException {
        WxMessageResult result = new WxMessageResult();
        result.senderId = "system";
        result.content = "消息不是json格式或格式有误";
        ObjectMapper mapper = new ObjectMapper();
        String jsonMessage = mapper.writeValueAsString(result);
        return new TextWebSocketFrame(jsonMessage);
    }





}
